<html>
<head><meta charset="utf-8"><title>Destructors in unsafe code · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Destructors.20in.20unsafe.20code.html">Destructors in unsafe code</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="199880520"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Destructors%20in%20unsafe%20code/near/199880520" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boats <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Destructors.20in.20unsafe.20code.html#199880520">(Jun 05 2020 at 14:04)</a>:</h4>
<p>So in writing ringbahn I've so far had 2 memory safety bugs, both of which were ultimately caused by destructors running in unsafe code that I wrote without properly considering the implicit destructors. It would be nice to have some sort of optional lint to warn about every destructor running in an unsafe scopes (which would naturally be silenced by introducing an explicit call to <code>drop</code>).</p>



<a name="199880682"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Destructors%20in%20unsafe%20code/near/199880682" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boats <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Destructors.20in.20unsafe.20code.html#199880682">(Jun 05 2020 at 14:05)</a>:</h4>
<p>One bug was the classic using assignment when I should've used ptr::write, the other was that I had <code>NonNull&lt;Mutex&lt;_&gt;&gt;</code> and did <code>as_ref().lock()</code>, then freed the mutex's backing memory without first dropping the lock.</p>



<a name="199880859"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Destructors%20in%20unsafe%20code/near/199880859" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boats <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Destructors.20in.20unsafe.20code.html#199880859">(Jun 05 2020 at 14:06)</a>:</h4>
<p>Unsafe code is often a situation where implicitly inserting code in an order that can be hard to determine is a recipe for disaster, and thats exactly what destructors do by default.</p>



<a name="199886687"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Destructors%20in%20unsafe%20code/near/199886687" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Destructors.20in.20unsafe.20code.html#199886687">(Jun 05 2020 at 14:46)</a>:</h4>
<p>There was a similar request for a clippy lint: <a href="https://github.com/rust-lang/rust-clippy/issues/3915">https://github.com/rust-lang/rust-clippy/issues/3915</a></p>



<a name="199886764"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Destructors%20in%20unsafe%20code/near/199886764" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Destructors.20in.20unsafe.20code.html#199886764">(Jun 05 2020 at 14:47)</a>:</h4>
<p><span aria-label="+1" class="emoji emoji-1f44d" role="img" title="+1">:+1:</span> for having a lint like that.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>